#!usr/bin/env python3
#-*- coding:utf-8 _*-
"""
@author:shilei
@file: mark_txt.py
@time: 2020/11/05

__author__ = "lei.shi@ximalaya.com"
"""

# -*- coding: utf-8 -*-

import os, sys
import time
f_1 = []
f_2 = []
def test_log():
    info = os.getcwd()+os.sep+'log'
    print(info)
    fout_1 = open('zlog_1.txt', 'w')  # 合并内容到该文件
    fout_2 = open('zlog_2.txt', 'w')  # 合并内容到该文件

    def writeintofile_1(info):
        with open(info, 'rb') as fp:
            for strinfo in fp.readlines():
                res_info = strinfo.decode("utf8", "ignore")
                # print(res_info)
                # print(res_info)_N_PROD1_NANO_TD_NANO01120B400004R_20201130_110804.log
                # _N_PROD1_NANO_TD_NANO01120B400004R_20201130_110952.log


                fout_1.write(res_info)
    def writeintofile_2(info):
        with open(info, 'rb') as fp:
            for strinfo in fp.readlines():
                res_info = strinfo.decode("utf8", "ignore")
                # print(res_info)
                # print(res_info)_N_PROD1_NANO_TD_NANO01120B400004R_20201130_110804.log
                # _N_PROD1_NANO_TD_NANO01120B400004R_20201130_110952.log


                fout_2.write(res_info)


    for root, dirs, files in os.walk(info):
        files.sort()
        len_num = len(files)
        print(len_num)
        if len_num > 40:
            a = files[:40]
            for i in a:
                f_1.append(i)
            # print(a)
            for fl in f_1:
                # print(fl)
                info = "%s/%s" % (root, fl)
                print(info)
                if os.path.splitext(info)[1] in ['.log',]:
                    # if info[-5:] == 'log__':  # 只将后缀名为log的文件内容合并
                    print(info)
                    # time.sleep(5)
                    writeintofile_1(info)
            b = files[40:]
            for i in b:
                f_2.append(i)
            print(b)
            for fl in f_2:
                # print(fl)
                info = "%s/%s" % (root, fl)
                # print(info)
                if os.path.splitext(info)[1] in ['.log',]:
                    # if info[-5:] == 'log__':  # 只将后缀名为log的文件内容合并
                    print(info)

                    # time.sleep(5)
                    writeintofile_2(info)
        else:

            for fl in files:
                # print(fl)
                info = "%s/%s" % (root, fl)
                # print(info)
                if os.path.splitext(info)[1] in ['.log',]:
                    # if info[-5:] == 'log__':  # 只将后缀名为log的文件内容合并
                    print(info)

                    # time.sleep(5)
                    writeintofile_1(info)
            os.remove('./zlog_2.txt')
    fout_1.close()
    fout_2.close()
test_log()
# for i in os.popen('du -h zlog.txt'):
#     print(i)
    # d_x = int(i.split()[0].split('.')[0])
    # print(d_x)
